<template>
  <el-main>
    <el-row class="body-card">
      <h3 style="cursor: pointer">好友请求列表</h3>
      <Divider/>
	    <el-row>
	   <el-col :span="8" v-for="(item, index) in friendrequestlist_items" :key="index" >  
		   <el-card :body-style="{ padding: '0px' }">
		    <div style="padding: 14px;">
        <span>  {{ item.username}} (用户id: {{ item.id}})</span>
        <div class="bottom clearfix">  
          <el-button type="text" @click="handleFriendAppend(item.id)">添加好友</el-button>
        </div> 
      </div> 
    </el-card>
  </el-col>
      </el-row>
   
    </el-row>
  </el-main>
</template>

<script>
import {
  friendrequestlistServer,
  friendServer, 
} from "../../api/friend";
  export default {
    name: "friendrequestmenu",
    data() {
      return {
        dialogFormVisible: false,
        dialogTypes: 0,
        friendrequestlist_items: [],
        formLabelWidth: '100px',
        formData: {
          id: 123,
          name: "张三",
          role: 'StandardUser'
        }
      };
    },
    mounted() {
      this.getfriendrequestlist();
    },
    methods: {
	handleFriendAppend(requestid){
	 if (!!requestid) { 
        this.$confirm("是否接受该好友申请?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          friendServer("put", {
            params: 
	   {requestid: requestid,} 
          }).then(
            suc => {
              const { code, message } = suc.data;
              if (code) {
                this.$message({
                  type: "success",
                  message
                });
				this.getfriendrequestlist();
              } else {
                this.$message({
                  type: "error",
                  message
                });
              }
            },
            err => {
              this.$message({
                type: "error",
                message: err.message
              });
            }
          );
        });
      }
	},
     getfriendrequestlist() {
       friendrequestlistServer("get", {}).then(
        suc => {
          const { code, data, message } = suc.data;
          if (code) {
            if (Array.isArray(data)) {
              this.friendrequestlist_items = data; 
            }
          } else {
            this.$message({
              type: "error",
              message
            });
          }
        },
        err => {
          this.$message({
            type: "error",
            message: err.message
          });
        }
      );
      },
    }
  };
</script>

<style lang="less">

</style>
